<template>
  <div class="app-container">

    <el-card class="box-card">
      <el-descriptions title="系统定价">
        <el-descriptions-item label="人才股票单价">{{ stockPrice }}元</el-descriptions-item>
      </el-descriptions>
    </el-card>

    <el-card class="box-card">
      <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px">
        <el-form-item label="发行状态" prop="issueType">
          <el-select v-model="queryParams.issueType" clearable placeholder="请选择发行状态" size="small">
            <el-option
              v-for="dict in issueTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="审核状态" prop="verifyType">
          <el-select v-model="queryParams.verifyType" clearable placeholder="请选择审核状态" size="small">
            <el-option
              v-for="dict in verifyTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['publish:issue:add']"
        >发行</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

      <el-table v-loading="loading" :data="historyList" @selection-change="handleSelectionChange">
        <el-table-column align="center" type="selection" width="55"/>
        <el-table-column :formatter="issueTypeFormat" align="center" label="发行状态" prop="issueType"/>
        <el-table-column align="center" label="资金用途" prop="issueUse" width="300"/>
        <el-table-column align="center" label="发行数量" prop="issueNumber" width="100"/>
        <el-table-column align="center" label="分红承诺" prop="issueBonus" width="500"/>
        <el-table-column :formatter="verifyTypeFormat" align="center" label="审核状态" prop="verifyType"></el-table-column>
        <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
          <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['publish:issue:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['publish:issue:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

      <pagination
        v-show="total>0"
        :limit.sync="queryParams.pageSize"
        :page.sync="queryParams.pageNum"
        :total="total"
        @pagination="getList"
      />

      <!-- 添加或修改发行历史对话框 -->
      <el-dialog :visible.sync="open" append-to-body title="发行" width="800px">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="发行状态" prop="issueType">
            <el-select v-model="form.issueType" placeholder="请选择发行状态">
              <el-option
                v-for="dict in issueTypeOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="parseInt(dict.dictValue)"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="资金用途" prop="issueUse">
            <el-input v-model="form.issueUse" placeholder="请输入资金用途"/>
          </el-form-item>
          <el-form-item label="发行数量" prop="issueNumber">
            <el-input v-model="form.issueNumber" placeholder="请输入发行数量"/>
          </el-form-item>
          <el-form-item label="分红承诺" prop="issueBonus">
            <el-input v-model="form.issueBonus" placeholder="请输入分红承诺" type="textarea"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel">关 闭</el-button>
          <el-button @click="submitForm">保 存</el-button>
          <el-button type="primary" @click="submit">提 交</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import {
  listHistory,
  getHistory,
  delHistory,
  addHistory,
  updateHistory,
  exportHistory,
  myListHistory
} from "@/api/publish/issue/issueHistory";
import {getMyResume} from "@/api/publish/resume";


export default {
  name: "History",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 发行历史表格数据
      historyList: [],
      // 弹出层标题
      title: "修改发行股票",
      // 是否显示弹出层
      open: false,
      // 发行状态字典
      issueTypeOptions: [],
      // 审核状态字典
      verifyTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        issueType: null,
        verifyType: null,
        issueUse: null,
        issueNumber: null,
        issueBonus: null,
        deptId: null,
        createById: null,
        updateById: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        issueType: [
          {required: true, message: "发行状态不能为空", trigger: "blur"}
        ],
        issueUse: [
          {required: true, message: "资金用途不能为空", trigger: "blur"}
        ],
        issueNumber: [
          {required: true, message: "发行数量不能为空", trigger: "blur"},
          {
            pattern: /(^[1-9]([0-9]{0,10})?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]{1,2}$)/,
            message: '请输入正确数量格式,可保留两位小数'
          }
        ],
        issueBonus: [
          {required: true, message: "分红承诺不能为空", trigger: "blur"}
        ],
      },
      // 股票价格
      stockPrice: 0,
    };
  },
  created() {
    this.getList();
    this.getDicts("issue_type").then(response => {
      this.issueTypeOptions = response.data;
    });
    this.getDicts("verify_type").then(response => {
      this.verifyTypeOptions = response.data;
    });

    // 获取股票定价
    getMyResume().then(response => {
      if (response && response.code === 200) {
        this.stockPrice = response.data.score;
      }
    })
  },
  methods: {
    /** 查询发行历史列表 */
    getList() {
      this.loading = true;
      myListHistory(this.queryParams).then(response => {
        this.historyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 发行状态字典翻译
    issueTypeFormat(row, column) {
      return this.selectDictLabel(this.issueTypeOptions, row.issueType);
    },
    // 审核状态字典翻译
    verifyTypeFormat(row, column) {
      return this.selectDictLabel(this.verifyTypeOptions, row.verifyType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        issueType: null,
        issueUse: null,
        issueNumber: null,
        verifyType: 0,
        issueBonus: null,
        deptId: null,
        createBy: null,
        createById: null,
        createTime: null,
        updateBy: null,
        updateById: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加发行历史";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getHistory(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改发行历史";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateHistory(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHistory(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    //提交进行审核
    submit() {
      this.form.verifyType="1";
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateHistory(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHistory(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除本次股票发行?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
        return delHistory(row.id);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
  }
};
</script>

<style scoped>
.box-card {
  margin: 20px 0;
}
</style>
